from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier #k临近算法
from sklearn.metrics import accuracy_score

#加载数据
x,y = datasets.load_iris(return_X_y=True)
#拆分数据集，留出法
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
#利用KNN对象训练模型
knn_estimator = KNeighborsClassifier(n_neighbors=6)
#利用训练好得模型进行预测
knn_estimator.fit(x_train, y_train)
#对预测结果进行评估
y_predict = knn_estimator.predict(x_test)

print("准确率:", sum(y_predict == y_test)/y_test.shape[0])
print("准确率2:", accuracy_score(y_predict, y_test))


